<template>
  <div class="cart" @mouseenter="isOpen = true">
        <RouterLink  class="curr" to="/cart">
          <i class="iconfont icon-cart"></i><em>{{$store.getters['cart/validTotal']}}</em>
        </RouterLink>
        <div class="layer" v-if="$store.getters['cart/validTotal'] > 0 && $route.path !== '/cart'" v-show=" isOpen === true">
            <div class="list">
                <RouterLink :to="`/product/${goods.id}`" @click="Btn" class="top" v-for="goods in $store.getters['cart/validList']" :key="goods.skuId">
                <img :src="goods.picture" alt="">
                <div class="midden">
                    <p class="one">{{goods.name}}</p>
                    <p class="ellipsis">{{goods.attrsText}}</p>
                </div>
                <div class="price">
                    <span><i>￥</i>{{goods.nowPrice}}</span>
                    <i>×{{goods.count}}</i>
                </div>
                <span class="qingchu" @click.prevent.stop="delte(goods.skuId)">×</span>
                </RouterLink>
            </div>
            <div class="bottom">
                <div class="left">
                    <p class="product">共&nbsp;{{$store.getters['cart/validTotal']}}&nbsp;件商品</p>
                    <p class="price"><span>￥</span>{{$store.getters['cart/validAmount']}}</p>
                </div>
                <div class="right">
                    <button @click="$router.push('/cart')" class="button">去购物车结算</button>
                </div>
            </div>
        </div>
      </div>
</template>

<script>
import { useStore } from 'vuex'
import { ref } from 'vue'
import Message from '@/components/libaray/Message'
export default {
  name: 'AppHeaderCart',
  setup () {
    const store = useStore()
    store.dispatch('cart/findCart').then(() => {
      Message({ type: 'success', message: '更新本地购物车成功' })
    })

    const delte = (skuId) => {
      store.dispatch('cart/deleteCart', skuId).then(() => {
        Message({ type: 'success', message: '删除成功' })
      })
    }
    const isOpen = ref(true)
    const Btn = () => {
      isOpen.value = false
    }
    return { delte, Btn, isOpen }
  }

}
</script>

<style scoped lang="less">
 .cart {
    width: 50px;
    position: relative;
    z-index: 600;
    &:hover .layer {
            opacity: 1;
            transform: none;
    }
    .curr {
      height: 32px;
      line-height: 32px;
      text-align: center;
      position: relative;
      display: block;
      .icon-cart{
        font-size: 22px;
      }
      em {
        font-style: normal;
        position: absolute;
        right: 0;
        top: 0;
        padding: 1px 6px;
        line-height: 1;
        background: @helpColor;
        color: #fff;
        font-size: 12px;
        border-radius: 10px;
        font-family: Arial;
      }
    }
    .layer {
        position: absolute;
        top: 50px;
        right: 0;
        width: 400px;
        height: 400px;
        // background-color: pink;
        transform: translate3d(0,-200px,0) scale(1,0);
        background-color: #fff;
        z-index: 200;
        padding: 20px 8px 0;
        opacity: 0;
        // overflow: hidden;
         box-shadow: 0 0 10px rgba(0,0,0,0.2);
        transition: all .4s .2s;
        .list {
            height: 310px;
            overflow: auto;
            &::-webkit-scrollbar{
      width:10px;
      height:10px;
    }
    &::-webkit-scrollbar-track{
      background: #f8f8f8;
      border-radius: 2px;
    }
    &::-webkit-scrollbar-thumb{
      background: #eee;
      border-radius:10px;
    }
    &::-webkit-scrollbar-thumb:hover{
      background: #ccc;
    }
        }
        &::before {
            content:'';
            position: absolute;
            right: 14px;
            top: -10px;
            width: 20px;
            height: 20px;
            background-color: #fff;
            transform: scaleX(.6) rotate(45deg);
            box-shadow: -3px -3px 5px rgb(0 0 0 / 10%);
        }
        .top {
            display: flex;
            align-items: center;
            padding: 10px 0;
            cursor: pointer;
            &:hover .qingchu {
                 opacity: 1;
                    cursor: pointer;
            }
            img {
                width: 80px;
                height: 80px;
            }
            .midden {
                width: 200px;
                padding: 0 10px;
                .one {
                    font-size: 16px;
                }
                .ellipsis {
                    font-size: 16px;
                    color: #999;
                    padding-top: 4px;
                }
            }
            .price {
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                margin-left: -16px;
                flex: 1;
                span {
                    color: @priceColor;
                    font-size: 16px;
                    i {
                        color: @priceColor;
                        letter-spacing: -2px;
                    }
                }
                i {
                    color: #666;
                    font-size: 16px;
                    padding-top: 6px;
                }
            }
            .qingchu {
                font-size: 25px;
                color: #666;
                opacity: 0;
                transition: all .5s;
            }

        }
        .bottom {
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: absolute;
            bottom: 0;
            left: 0;
            height: 60px;
            padding: 0 8px 0 20px;
            width: 100%;
            z-index: 220;
            background-color: #f8f8f8;
            .right {
                .button {
                    height: 50px;
                    width: 180px;
                    font-size: 16px;
                    color: @xtxColor;
                    background-color: #e6faf6;
                    border: 1px solid  @xtxColor;
                    border-radius: 5px;
                    cursor: pointer;
                }
            }
            .left {
                .product {
                    color: #999;

                }
                .price {
                    color: @priceColor;
                    font-size: 18px;
                    margin-left: -3px;
                    // letter-spacing: ;
                    span {
                        letter-spacing: -4px;
                    }
                }
            }
        }
    }
  }
</style>
